﻿using Leo.Awesome.AppPlatform.Consts;
using Leo.Awesome.AppPlatform.Domain;
using Leo.Awesome.AppPlatform.DomainServices;
using N6.Client.AppPlatform;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;

namespace Leo.Awesome.AppPlatform.Seeds.Menus
{
    public class MenuDataSeedContributor :
        IDataSeedContributor,
        ITransientDependency
    {
        private readonly IMenuDataSeeder MenuDataSeeder;
        public MenuDataSeedContributor(IMenuDataSeeder MenuDataSeeder)
        {
            this.MenuDataSeeder = MenuDataSeeder;
        }
        public async Task SeedAsync(DataSeedContext context)
        {
            var readMenus = await ReadMenus(".//Seeds//Menus//MenuDataSeed.json");
            var MenusList = readMenus.ToList(node => node.Items);
            MenusList = MenusList.ToList(node => node.Items);
            await MenuDataSeeder.SeedAsync(MenusList, AppPlatformServiceConsts.Domain);
        }

        public async Task<List<Menu>> ReadMenus(string filePath)
        {
            var jsonContent = System.IO.File.ReadAllText(filePath);
            var Menus = JsonConvert.DeserializeObject<List<Menu>>(jsonContent);
            var menusFromDb = await MenuDataSeeder.GetAllListAsync(AppPlatformServiceConsts.Domain);
            var results = ReadMenusRecursive(Menus, menusFromDb, null);
            return results;
        }

        private static List<Menu> ReadMenusRecursive(List<Menu> Menus, List<Menu> menusfromDb, Menu parent = null)
        {
            var results = new List<Menu>();
            var index = 1;
            foreach (var Menu in Menus)
            {
                var id = Guid.Empty;
                var pathTree = string.Empty;
                Guid? parentId = null;
                var menufromDb = menusfromDb.FirstOrDefault(x => x.Code == Menu.Code);
                if (menufromDb != null)
                {
                    id = menufromDb.Id;
                    pathTree = menufromDb.PathCode;
                    parentId = menufromDb.ParentId;
                }
                else
                {
                    id = Guid.NewGuid();
                    pathTree = parent?.PathCode + id + "/";
                    parentId = parent?.Id;
                }
                var rootMenu = new Menu(
                    id,
                    parentId,
                    Menu.Name,
                    Menu.Code,
                    pathTree,
                    Menu.Permission,
                    Menu.MenuType,
                    index,
                    Menu.RouterPath,
                    Menu.RouterName,
                    Menu.RouterComponent,
                    Menu.Meta,
                    Menu.Display,
                    Menu.Url);

                results.Add(rootMenu);

                if (Menu.Items != null && Menu.Items.Count > 0)
                {
                    var childMenus = ReadMenusRecursive(Menu.Items, menusfromDb, rootMenu);
                    rootMenu.Items.AddRange(childMenus);
                }
                index += 10;
            }

            return results;
        }
    }
}
